﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.BBS;

namespace Marbles.Web.Admin.Content
{
    public partial class BBSCategory : ContentBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            cPnlSites.Visible = Page.AuthenticationProvider.CurrentToken.IsRoot;

            if (!Page.IsPostBack)
            {
                InitialiseForm();
            }

            LoadData();
        }

        private void InitialiseForm()
        {
            cDrpSite.DataSource = new Core.Content.Sites.SiteDefinitionCollection();
            cDrpSite.DataBind();
            cDrpSite.SelectedValue = this.CurrentWeb.SiteId;
        }

        protected void cDrpSite_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            cGrdCat.DataSource = new ForumCategoryCollection(cDrpSite.SelectedValue);
            cGrdCat.DataBind();
        }

        private void ClearDialog()
        {
            cTxtCategory.Text = "";
            cTxtTitle.Text = "";
            cTxtCategory.Enabled = true;
        }

        protected void Page_Command(object sender, CommandEventArgs e)
        {
            string categoryname = e.CommandArgument.ToString();
            ForumCategory cat = null;

            switch (e.CommandName.ToLower())
            {
                case "editcommand":
                    cat = new ForumCategory(cDrpSite.SelectedValue, categoryname);
                    ClearDialog();
                    cDlgEditCat.Visible = true;
                    cBtnDelete.CommandArgument = categoryname;
                    cBtnSave.CommandArgument = categoryname;
                    cBtnSave.CommandName = "Update";
                    cBtnDelete.Visible = true;
                    cDlgEditCat.Title = "Edit Category";
                    cTxtTitle.Text = cat.Title;
                    cTxtCategory.Text = cat.Category;
                    cTxtCategory.Enabled = false;                    
                    break;
                case "newcategory":
                    cDlgEditCat.Visible = true;
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    cBtnSave.CommandName = "Insert";
                    cBtnDelete.Visible = false;
                    cDlgEditCat.Title = "New Category";
                    ClearDialog();

                    break;
                case "update":
                    cat = new ForumCategory(cDrpSite.SelectedValue, e.CommandArgument.ToString());
                    cat.Title = cTxtTitle.Text;
                    cat.Update();
                    LoadData();
                    cDlgEditCat.Visible = false;
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    break;
                case "insert":
                    cat = ForumCategory.CreateNew(cDrpSite.SelectedValue, cTxtCategory.Text.Trim());
                    cat.Title = cTxtTitle.Text;
                    cat.Insert();
                    LoadData();
                    cDlgEditCat.Visible = false;
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    break;
                case "cancel":
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    cDlgEditCat.Visible = false;
                    LoadData();
                    break;
                case "delete":
                    cat = new ForumCategory(cDrpSite.SelectedValue, e.CommandArgument.ToString());
                    cat.Delete();
                    LoadData();
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    cDlgEditCat.Visible = false;
                    break;
            }
        }

    }
}